//
//  PickerViewManager.m
//  LearnObjectiveC
//
//  Created by 吴可高 on 16/5/11.
//  Copyright © 2016年 吴可高. All rights reserved.

#import "PickerViewManager.h"
#import "PickerSourceModel.h"
#import "MunicipalModel.h"

static PickerViewManager * _pickerViewManager = nil;
@interface PickerViewManager ()<UIPickerViewDataSource,UIPickerViewDelegate,UIPickerViewAccessibilityDelegate>
{
    CGRect _pickerViewFrame;
}
@property (copy, nonatomic) PickerViewResponseBlock block;
@property (strong, nonatomic)PickerSourceModel * pickerModel;
@property (strong, nonatomic) NSMutableArray * provinceArray;
@property (strong, nonatomic) NSMutableArray * cityArray;
@property (strong, nonatomic) NSMutableArray * villagesArray;

@end

@implementation PickerViewManager

- (instancetype)init
{
    self = [super init];
    if (self) {
        if (!_pickerViewManager) {
            _pickerViewManager  = [[PickerViewManager alloc]init];
        }
    }
    return _pickerViewManager;
}

-(instancetype)initWithPickerViewSourceModel:(PickerSourceModel*)sourceModel frame:(CGRect)frame andresponseBlcok:(PickerViewResponseBlock)reponseBlock;
{
    if (self = [super init])
    {
        _block = reponseBlock;
        _pickerViewFrame  = frame;
        _pickerModel  = sourceModel;
        [self setup];
    }
    return self;
}

-(UIPickerView *)pickerView
{
    _pickerView = ({
        if (!_pickerView) {
            _pickerView  = [[UIPickerView alloc]initWithFrame:_pickerViewFrame];
            _pickerView.delegate   = self;
            _pickerView.dataSource = self;
        }
        _pickerView;
    });
    return _pickerView;
}

-(void)setItems:(NSArray *)items
{
    _items = items;
    _provinceArray = [NSMutableArray arrayWithArray:items];
    _cityArray  = [NSMutableArray arrayWithArray:[_items[0]twoLevels]];
    _villagesArray  = [NSMutableArray arrayWithArray:[_items[0]threeLevels]];
    [_pickerView reloadAllComponents];
}
-(void)setup
{
    [_pickerView selectRow:0 inComponent:0 animated:YES];
    [_pickerView reloadAllComponents];

}
#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
    return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
    if (component ==0) {
        return _provinceArray.count;
    }
    if (component ==1)
    {
        return _cityArray.count;
       
    }
    if (component  == 2)
    {
        return _villagesArray.count;
    }
    return 0;
}

#pragma mark -UIPickerViewDelegate
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    return kScreen_Width/3.f;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 35.f;
}
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component __TVOS_PROHIBITED;
{
    if (component ==0)
    {
        PickerSourceModel * model = _provinceArray[row];
        return model.name;
    }
    if (component ==1) {
        PickerSourceModel * model =  _cityArray[row];
        return model.name;
    }
    if (component ==2)
    {
        MunicipalModel * model =  _villagesArray[row];
        return model.name;
    }
    return nil;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view;
{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        pickerLabel.textAlignment=NSTextAlignmentCenter;
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    }
    return pickerLabel;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component __TVOS_PROHIBITED;
{
   
    if (component ==0) {
        [_pickerView selectRow:0 inComponent:1 animated:YES];
        [_pickerView selectRow:0 inComponent:2 animated:YES];
        [_cityArray removeAllObjects];
        [_villagesArray removeAllObjects];
        _cityArray = [NSMutableArray arrayWithArray:[_provinceArray[row]twoLevels]];
        _villagesArray = [NSMutableArray arrayWithArray:[_provinceArray[row]threeLevels]];
        if (_cityArray.count ==0)
        {
            PickerSourceModel * model =[PickerSourceModel new];
            model.name = @"全部";
            model.enName = @"全部";
            model.next =@"NULL";
            model.idx = 0;
            [_cityArray addObject:model];
            
        }
        if (_villagesArray.count ==0)
        {
            MunicipalModel * model = [MunicipalModel new];
            model.name = @"全部";
            model.enName = @"全部";
            model.idx = 0;
            [_villagesArray addObject:model];
        }
        [_pickerView reloadComponent:1];
        [_pickerView reloadComponent:2];

    }
    if (component ==1)
    {
        [_pickerView selectRow:0 inComponent:component + 1 animated:YES];
        [_villagesArray removeAllObjects];
        _villagesArray = [NSMutableArray arrayWithArray:[_provinceArray[row]threeLevels]];
        if (_villagesArray.count ==0)
        {
            MunicipalModel * model = [MunicipalModel new];
            model.name = @"全部";
            model.enName = @"全部";
            model.idx = 0;
            [_villagesArray addObject:model];
        }
        [_pickerView reloadComponent:2];
    }
    if (component ==2) {
        [_pickerView selectRow:row inComponent:component animated:YES];
    }
}
#pragma mark - UIPickerViewAccessibilityDelegate
- (nullable NSString *)pickerView:(UIPickerView *)pickerView accessibilityLabelForComponent:(NSInteger)component;
{
    NSInteger row = [_pickerView selectedRowInComponent:component];
    PickerSourceModel * model = _items[row];
    return   model.name;
}
- (nullable NSString *)pickerView:(UIPickerView *)pickerView accessibilityHintForComponent:(NSInteger)component;
{
    NSInteger row = [_pickerView selectedRowInComponent:component];
    PickerSourceModel * model = _items[row];
    return   model.name;
}
@end
